/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.model
{

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

import org.teotigraphix.dx.core.ToolKitUtil;
import org.teotigraphix.dx.core.dx_internal;
import org.teotigraphix.dx.interfaces.IActivate;
import org.teotigraphix.dx.interfaces.IComponentAware;
import org.teotigraphix.dx.interfaces.IComponentLayerClient;
import org.teotigraphix.dx.interfaces.IComponentModel;

/**
 * Default implementation of the <code>IComponentModel</code> interface
 * used within an <code>IModelLayerAware</code> component.
 * 
 * <p>Any properties defined on this class will be servicalbe state properties
 * that cooperate with the Component's public API. Any methods defined on 
 * this class will be servicable state methods or wrapping control methods.</p>
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @date 07-08-09
 */
public class ComponentModel 
	   extends EventDispatcher
	   implements IComponentAware, IComponentModel, 
				  IComponentLayerClient, IActivate
{
	include '../../core/Version.as'
	
	/**
	 * @private
	 */
	dx_internal var resetComponent:Boolean = false;
	
	/**
	 * @private
	 */
	dx_internal var initializeComponent:Boolean = false;
	
	//--------------------------------------------------------------------------
	//
	//  IActivate API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  isActive
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _isActive:Boolean = false;

	/**
	 * @copy org.teotigraphix.dx.interfaces.IComponentLayer#isActive
	 * 
	 * @uses #commitComponent()
	 */
	public function get isActive():Boolean
	{
		return _isActive;
	}
	
	//--------------------------------------------------------------------------
	//
	//  IComponentAware API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  component
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _component:IEventDispatcher;
	
	/**
	 * @private
	 */
	private var oldComponent:IEventDispatcher;
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IComponentAware#component
	 * 
	 * @uses #commitComponent()
	 */
	public function get component():IEventDispatcher
	{
		return _component;
	}
	
	/**
	 * @private
	 */	
	public function set component(value:IEventDispatcher):void
	{
		oldComponent = _component;
		_component = value;
		commitComponent();
		oldComponent = null;
	}
	
	//----------------------------------
	//  className
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _className:String;
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IComponentAware#className
	 */
	public function get className():String
	{
		return _className;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ComponentModel()
	{
		super();
		
		_className = ToolKitUtil.getClassName(this);
	}
	
	//--------------------------------------------------------------------------
	//
	//  IComponentService API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IComponentModel#listValueInterests()
	 */
	public function listValueInterests():Array
	{
		return null;
	}
	
	//--------------------------------------------------------------------------
	//
	//  IActivate API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IActivate#activate()
	 */
	public function activate(host:IEventDispatcher):void
	{
		_isActive = true;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IActivate#activate()
	 */
	public function deactivate(host:IEventDispatcher):void
	{
		_isActive = false;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Commits the <code>component</code> property.
	 * 
	 * <p>Override in subclasses to update when the <code>component</code> property
	 * has changed.</p>
	 */
	protected function commitComponent():void
	{
	}
}
}